// pages/cart/cart.js
import {_getCart} from '../../api/details'
import {editNumber,delCart} from '../../api/cart'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shippingCarInfo:{},
    saveHidden: true,
    allSelect: true,
    noSelect: false,
    delBtnWidth: 120, //删除按钮宽度单位（rpx）
  },
  toOrder(){ //跳转确认订单
      wx.navigateTo({
        url:"/pages/order/order"
      })
  },
   toDel(e){ //删除接口
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      complete:async (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          let res=await delCart(e.currentTarget.dataset.key)
          console.log(res,"----------")
          this.getMycart();//删除成功 从新获取列表
        }
      }
    })
  
  },
  async minOrPlus(e){ //修改数量的方法
    let {sta,number,key}=e.currentTarget.dataset;
    if(sta=="min"){
      --number
      if(number<=1){
        wx.showToast({
          title: '不能在减了',
        })
        return 
      }
    }else if(sta=="plus"){
      ++number
    }
    let res=await editNumber({
      token:wx.getStorageSync("token"),
      key:key,
      number:number
    })
    console.log(res)
    this.getMycart();
  },
   //获取元素自适应后的实际宽度
   getEleWidth: function(w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth
      var scale = (750 / 2) / (w / 2)
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function() {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  touchS: function(e) { //开始触摸
    if (e.touches.length == 1) {
      // 收集到开始的x位置
      this.setData({
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function(e) {
    const index = e.currentTarget.dataset.index;
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth; //120rpx
      var left = "";
      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，container位置不变
        left = "margin-left:0px";
      } else if (disX > 0) { //移动距离大于0，container left值等于手指移动距离
        left = "margin-left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          left = "left:-" + delBtnWidth + "px";
        }
      }
      this.data.shippingCarInfo.items[index].left = left
      this.setData({
        shippingCarInfo: this.data.shippingCarInfo
      })
    }
  },

  touchE: function(e) {
    var index = e.currentTarget.dataset.index;
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var left = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
      console.log(this.data.shippingCarInfo)
      this.data.shippingCarInfo.items[index].left = left
      this.setData({
        shippingCarInfo: this.data.shippingCarInfo
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.initEleWidth();
  },
  async getMycart(){
    let res=await _getCart();
    console.log(res,"-----");
    this.setData({
      shippingCarInfo:res.data
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMycart();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})